/* icstjrrp.p  - STOCK JOURNAL BY PART / LOCATION / EFFECTIVE DATE            */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: icstjrrp.p 2301 2010-11-08 07:09:06Z jvz                          $: */
/* $Revision: 1.16 $                                                          */
/* Revision: 1.8        BY: Karel Groos         DATE: 04/16/02   ECO: *N1B4*  */
/* Revision: 1.9        BY: Patrick de Jong     DATE: 05/31/02   ECO: *P07H*  */
/* Revision: 1.11       BY: Paul Donnelly (SB)  DATE: 06/28/03   ECO: *Q00G*  */
/* Revision: 1.12       BY: Sushant Pradhan     DATE: 09/13/04   ECO: *P2K1*  */
/* Revision: 1.13       BY: Priya Idnani        DATE: 01/10/05   ECO: *P33B*  */
/* Revision: 1.14       BY: Priyank Khandare    DATE: 04/12/05   ECO: *P3GF*  */
/* Revision: 1.15       BY: Steve Nugent        DATE: 06/14/05   ECO: *P3ND*  */
/* $Revision: 1.16 $    BY: Dhananjay Govekar DATE: 03/23/07  ECO: *P5RM*   */
/*-Revision end---------------------------------------------------------------*/

/*V8:ConvertMode=Report                                                       */

{us/mf/mfdtitle.i "1+ "}

define variable nbr      like tr_nbr no-undo.
define variable nbr1     like tr_nbr no-undo.
define variable part     like tr_part no-undo.
define variable part1    like tr_part no-undo.
define variable effdate  like tr_effdate no-undo.
define variable effdate1 like tr_effdate no-undo.
define variable site     like tr_site no-undo.
define variable site1    like tr_site no-undo.
define variable loc      like tr_loc no-undo.
define variable loc1     like tr_loc no-undo.
define variable newpage  like mfc_logical no-undo.
define variable desc1    like pt_desc1 no-undo.
define variable qtyin    like tr_qty_loc  no-undo.
define variable qtyout   like tr_qty_loc no-undo.
define variable newtrloc like mfc_logical no-undo.
define variable firstprinted like mfc_logical no-undo.
define variable endbalance like in_qty_oh  no-undo.
define variable receiver   as character initial "07" no-undo.
define variable l_item_deleted like mfc_logical no-undo.
define buffer trhist for tr_hist.

form
   part           colon 20
   part1          label "To" colon 49 skip
   effdate        colon 20
   effdate1       label "To" colon 49 skip
   site           colon 20
   site1          label "To" colon 49 skip
   loc            label "Location" colon 20
   loc1           label "To" colon 49 skip
   newpage        label "New Page Per Item/Location" colon 40
with frame a side-labels width 80 attr-space.

setFrameLabels(frame a:handle).

form
   qtyin        column-label "Quantity In"
   qtyout       column-label "Quantity Out"
   endbalance   column-label "Inventory"
   tr_effdate
   tr_date
   tr_type      column-label "TT"
   tr_trnbr
   tr_addr  format "x(10)"
   tr_ship_id
with frame b.

setFrameLabels(frame b:handle).

{us/wb/wbrp01.i &io-frame = "a"}

repeat:

   if part1 = hi_char then part1 = "".
   if effdate = low_date then effdate = ?.
   if effdate1 = hi_date then effdate1 = ?.
   if site1 = hi_char then site1 = "".
   if loc1 = hi_char then loc1 = "".

   if c-application-mode <> "WEB" then
      update
         part part1
         effdate effdate1
         site site1 loc loc1
         newpage
      with frame a.

   {us/wb/wbrp06.i &command = update
      &fields  = "part part1
        effdate effdate1
        site site1 loc loc1
        newpage"
      &frm     = "a"}

   bcdparm = "".
   {us/mf/mfquoter.i part    }
   {us/mf/mfquoter.i part1   }
   {us/mf/mfquoter.i effdate }
   {us/mf/mfquoter.i effdate1}
   {us/mf/mfquoter.i site    }
   {us/mf/mfquoter.i site1   }
   {us/mf/mfquoter.i loc     }
   {us/mf/mfquoter.i loc1    }
   {us/mf/mfquoter.i newpage }

   if part1 = ""   then part1 = hi_char.
   if effdate = ?  then effdate = low_date.
   if effdate1 = ? then effdate1 = hi_date.
   if site1 = ""   then site1 = hi_char.
   if loc1 = ""    then loc1 = hi_char.

   {us/gp/gpselout.i &printType = "printer"
               &printWidth = 132
               &pagedFlag = " "
               &stream = " "
               &appendToFile = " "
               &streamedOutputToTerminal = " "
               &withBatchOption = "yes"
               &displayStatementType = 1
               &withCancelMessage = "yes"
               &pageBottomMargin = 6
               &withEmail = "yes"
               &withWinprint = "yes"
               &defineVariables = "yes"}

   {us/bbi/mfphead.i}

   view frame b.
   for each tr_hist
      fields (tr_addr tr_date tr_domain tr_effdate tr_line tr_loc tr_lot
              tr_part tr_qty_loc tr_rmks tr_ship_id tr_ship_type tr_site
              tr_trnbr tr_type tr_um)
      where tr_hist.tr_domain = global_domain
      and ((tr_part          >= part and
            tr_part          <= part1)
      and   tr_effdate       <= effdate1
      and  (tr_site          >= site and
            tr_site          <= site1)
      and  (tr_loc           >= loc  and
            tr_loc           <= loc1)
      and   tr_ship_type     <> "M"
      and  (tr_type          <> "RCT-PO" or
            tr_ship_type     <> "s")
      and  (tr_type          <> "ISS-PRV" or
            tr_ship_type     <> "s"))
   use-index tr_part_eff no-lock
      break by tr_part by tr_site by tr_loc
            by tr_effdate descending
            by tr_trnbr descending
   with frame b down width 132 no-attr-space:

      if first-of(tr_loc)
      then do:

         assign
            firstprinted     = no
            newtrloc         = true
            endbalance       = 0
            l_item_deleted   = no.

        /* CHECK WHETHER ITEM IS HAVING TRANSACTIONS IN GIVEN DATE RANGE */
        /* AND WHETHER IT HAS BEEN DELETED                               */

         if not can-find(first trhist
                            where trhist.tr_domain   = tr_hist.tr_domain
                            and   trhist.tr_part     = tr_hist.tr_part
                            and  (trhist.tr_effdate >= effdate and
                                  trhist.tr_effdate <= effdate1)
                            and   trhist.tr_site     = tr_hist.tr_site
                            and   trhist.tr_loc      = tr_hist.tr_loc)
            and not can-find(first in_mstr
                                where in_domain = global_domain
                                and   in_part   = tr_hist.tr_part
                                and   in_site   = tr_hist.tr_site)
         then
            l_item_deleted = yes.

         if not l_item_deleted
         then do:

            for each ld_det
               fields (ld_domain ld_loc ld_part ld_qty_oh ld_site)
               where ld_det.ld_domain = global_domain
               and   ld_part          = tr_part
               and   ld_site          = tr_site
               and   ld_loc           = tr_loc
            no-lock use-index ld_part_loc:
               endbalance = endbalance + ld_qty_oh.
            end. /* FOR EACH ld_det */

            /* BACKOUT TRANSACTION QUANTITIES HAVING TRANSACTION */
            /* EFFECTIVE DATE GREATER THAN EFFDATE1 TO DISPLAY   */
            /* QUANTITY TILL EFFDATE1                            */

            if can-find(first trhist
                           where trhist.tr_domain   = tr_hist.tr_domain
                           and   trhist.tr_part     = tr_hist.tr_part
                           and   trhist.tr_effdate  > effdate1
                           and   trhist.tr_site     = tr_hist.tr_site
                           and   trhist.tr_loc      = tr_hist.tr_loc)
            then do:

               for each trhist
                  fields (tr_addr tr_date tr_domain tr_effdate tr_line tr_loc
                          tr_lot tr_part tr_qty_loc tr_rmks tr_ship_id
                          tr_ship_type tr_site tr_trnbr tr_type tr_um)
                  where trhist.tr_domain  = tr_hist.tr_domain
                  and   trhist.tr_part    = tr_hist.tr_part
                  and   trhist.tr_effdate > effdate1
                  and   trhist.tr_site    = tr_hist.tr_site
                  and   trhist.tr_loc     = tr_hist.tr_loc
                  and   tr_ship_type      <> "M"
                  and  (tr_type           <> "RCT-PO"
                     or tr_ship_type      <> "s")
                  and  (tr_type           <> "ISS-PRV"
                     or tr_ship_type      <> "s") 
               no-lock:

                  endbalance = endbalance - trhist.tr_qty_loc.

               end. /* FOR EACH trhist */
            end. /* CAN-FIND trhist */
         end. /* IF NOT l_item_deleted */
      end. /* FIRST-OF tr_loc */

      if     newtrloc
         and not l_item_deleted
      then do:

         if page-size - line-counter < 6
         then do:
            page.
            view frame b.
         end. /* IF page-size .. */

         desc1 = "".
         for first pt_mstr
            fields (pt_desc1 pt_desc2 pt_domain pt_part)
            where pt_mstr.pt_domain = global_domain
            and   pt_part           = tr_part
         no-lock:
            desc1 = pt_desc1 + " " + pt_desc2.
         end. /* FOR FIRST pt_mstr */

         if endbalance <> 0
            or can-find(first ld_det
                           where ld_domain = global_domain
                           and   ld_part   = tr_hist.tr_part
                           and   ld_site   = tr_hist.tr_site
                           and   ld_loc    = tr_hist.tr_loc)
            or can-find(first trhist
                           where trhist.tr_domain      = tr_hist.tr_domain
                           and   trhist.tr_part        = tr_hist.tr_part
                           and  (    trhist.tr_effdate >= effdate
                                 and trhist.tr_effdate <= effdate1)
                           and   trhist.tr_site        = tr_hist.tr_site
                           and   trhist.tr_loc         = tr_hist.tr_loc
                           and   trhist.tr_qty_loc     <> 0)
         then do:
            put
               {us/bbi/gplblfmt.i &FUNC=getTermLabel(""ITEM"",4) &CONCAT="":""}
               " " tr_part " "
               desc1 format "x(49)" " "
               {us/bbi/gplblfmt.i &FUNC=getTermLabel(""UM"",2) &CONCAT="":""}
               " " tr_um " "
               {us/bbi/gplblfmt.i &FUNC=getTermLabel(""SITE"",4) &CONCAT="': '"}
               tr_site " "
               {us/bbi/gplblfmt.i &FUNC=getTermLabel(""LOCATION"",4) &CONCAT="': '"}
               " " tr_loc skip.

            display
               getTermLabelRt("CLOSING",14) format "x(14)" @ qtyin
               getTermLabel("INVENTORY",14) format "x(14)" @ qtyout
               endbalance
            with frame b.
            down 1 with frame b.

            firstprinted = yes.
         end. /* IF CAN-FIND */
      end. /* IF newtrloc and .. */

      newtrloc = false.

      if page-size - line-counter < 2
      then
         page.

      assign
         qtyin  = 0
         qtyout = 0.

      /* DISPLAY TRANSACTIONS WITHIN DATE RANGE */

      if     tr_effdate >= effdate
         and tr_qty_loc <> 0.0
      then do:

         if     not (tr_type      = "RCT-PO" and
                     tr_ship_type = "s")
            and not (tr_type      = "ISS-PRV" and
                     tr_ship_type = "s")
         then do:
            if tr_qty_loc >= 0
            then
               qtyin  = tr_qty_loc.
            if tr_qty_loc <  0
            then
               qtyout = tr_qty_loc.
         end. /*IF NOT(tr_type = "RCT-PO" ... */

         if qtyin <> 0 or qtyout <> 0
         then do:

            if firstprinted
            then do:
               display
                  qtyin  when (tr_ship_type = "")
                  qtyout when (tr_ship_type = "")
                  endbalance
                  tr_effdate
                  tr_date
                  tr_type
                  tr_trnbr
                  tr_addr  format "x(10)"
                  tr_ship_id
               with frame b.
               down 1 with frame b.
            end. /* IF firstprinted */
         end. /* IF qtyin <> 0.0 or ... */

         /* COMPUTE THE BEGINNING BALANCE */
         if     not (tr_type      = "RCT-PO" and
                     tr_ship_type = "s" )
            and not (tr_type      = "ISS-PRV" and
                     tr_ship_type = "s")
         then
            endbalance = endbalance - tr_qty_loc.
      end. /* IF tr_effdate >= effdate ... */

      if last-of(tr_loc)
         and not l_item_deleted
         and firstprinted
      then do:
         /* DISPLAY INITIAL BALANCE */
         firstprinted = no.
         display
            getTermLabelRt("OPENING",14) format "x(14)" @ qtyin
            getTermLabel("INVENTORY",14) format "x(14)" @ qtyout
            endbalance
         with frame b.
         down 2 with frame b.
         if newpage
         then
            page.
      end. /* IF LAST-OF(tr_loc)... */

      {us/mf/mfrpchk.i}

   end. /* FOR EACH tr_hist */

   {us/mf/mfrtrail.i}

end. /* REPEAT */
